package com.changgou.goods.controller;

import com.changgou.goods.pojo.Album;
import com.changgou.goods.service.AlbumService;
import com.github.pagehelper.PageInfo;
import entity.Result;
import entity.StatusCode;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;


import java.util.List;

/**
 * 图片访问层
 * @author zhengjd
 * @date 2020/5/2 10:35
 */
@RestController
@RequestMapping("album")
@CrossOrigin
public class AlbumController {
    @Autowired
    private AlbumService albumService;


    /***
     * Album多条件分页查询
     * @param album
     * @param page
     * @param size
     * @return
     */
    @PostMapping("search/{page}/{size}")
    public Result<PageInfo<Album>> findPage(@RequestBody(required = false) Album album, @PathVariable int page, @PathVariable int size) {
        PageInfo<Album> pageInfo = albumService.findPage(album, page, size);
        return new Result<PageInfo<Album>>(true, StatusCode.OK,"查询成功",pageInfo);
    }
    /***
     * Album分页查询
     * @param page
     * @param size
     * @return
     */
    @GetMapping("search/{page}/{size}")
    public Result<PageInfo> findPage(@PathVariable int page, @PathVariable int size) {
        PageInfo<Album> pageInfo = albumService.findPage(page, size);
        return new Result<>(true, StatusCode.OK,"查询成功",pageInfo);
    }

    /***
     * Album多条件搜索方法
     * @param album
     * @return
     */
    @PostMapping("search")
    public Result<List<Album>> findList(@RequestBody Album album) {
        List<Album> list = albumService.findList(album);
        return new Result<List<Album>>(true, StatusCode.OK,"查询成功",list);
    }
    /***
     * 删除Album
     * @param id
     */
    @DeleteMapping("{id}")
    public Result delete(@PathVariable Long id) {
        albumService.delete(id);
        return new Result<>(true, StatusCode.OK,"删除成功");
    }
    /***
     * 修改Album数据
     * @param album
     */
    @PutMapping("{id}")
    public Result update(@RequestBody Album album, @PathVariable Long id) {
        //设置主键
        album.setId(id);
        albumService.update(album);
        return new Result<>(true, StatusCode.OK,"修改成功");
    }
    /***
     * 新增Album
     * @param album
     */
    @PostMapping
    public Result add(@RequestBody Album album) {
        albumService.add(album);
        return new Result<>(true, StatusCode.OK,"新增成功");
    }
    /**
     * 根据ID查询Album
     * @param id
     * @return
     */
    @GetMapping("{id}")
    public Result<Album> findById(@PathVariable Long id) {
        Album album = albumService.findById(id);
        return new Result<Album>(true, StatusCode.OK,"查询成功",album);
    }
    /***
     * 查询所有Album
     * @return
     */
    @GetMapping
    public Result<List<Album>> findAll() {
        List<Album> list = albumService.findAll();
        return new Result<List<Album>>(true, StatusCode.OK,"查询成功",list);
    }
}
